package org.jfk.tbox.net.app;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jfk.core.utils.ByteUtils;
import org.jfk.tbox.net.TboxMsg;

public class TBOX_AbnormalAlarm {
	static final String appId = "0601";
	
	static final Map<String,String> eventMap = new HashMap();
	
	static {
		eventMap.put("1", "TBOX电量不足");
		eventMap.put("2", "车辆异常情况报警");
		eventMap.put("3", "TBOX异常情况报警");
	}
	
	public class TBOX_AbnormalAlarmReq extends TboxMsg{
		
		byte alarmType = 0x00;

		byte isLocation = 0x00;
		double lng,lat;
		int heading,speed;
		
		Date gpsTime = null;
		
		public int getAlarmType(){
			return (int)alarmType;
		}
	
		public TBOX_AbnormalAlarmReq(){
			super.appId = TBOX_AbnormalAlarm.appId;
			super.msgId = 1;
		}
		
		public void loadData(byte[] data){
			super.loadData(data);
			byte[] _lat  = new byte[4] ,
					_lng  = new byte[4] ;
			byte[] _speed = new byte[2],
					_heading = new byte[2];

			
			byte[] _appData = super.appData;
			int idx = 0;
			
			alarmType = _appData[idx];
			idx = idx + 1;
			
			isLocation = _appData[idx];

			System.arraycopy(_appData, idx , _lat, 0 , _lat.length);
			idx = idx + _lat.length;
			lat = ByteUtils.byte4Tolong(_lat);

			System.arraycopy(_appData, idx , _lng, 0 , _lng.length);
			idx = idx + _lng.length;
			lng = ByteUtils.byte4Tolong(_lng);
			
			System.arraycopy(_appData, idx , _heading, 0 , _heading.length);
			idx = idx + _heading.length;
			heading = ByteUtils.byte2ToInt(_heading);

			System.arraycopy(_appData, idx , _speed, 0 , _speed.length);
			idx = idx + _speed.length;
			speed = ByteUtils.byte2ToInt(_speed);
			
		}
		
		public byte[] getAppData(){
			return null;
		}		
		
		public byte[] getResp(){
			TBOX_AbnormalAlarmResp resp = new TBOX_AbnormalAlarmResp();
			super.initResp(resp);
			return resp.toBytes();
		}	
		
		public String getEventDesc(){
			
			return eventMap.get(String.valueOf(this.alarmType));
		}		
		public String toString(){
			return "alarmType = " + this.alarmType + " , "
					+ "lng = " + this.lng + " , "
					+ "lat = " + this.lat  + " , " 
					+ "isLocation = " + this.isLocation  + " , " 
					+ "speed = " + this.speed; 
		}
	}

	public class TBOX_AbnormalAlarmResp extends TboxMsg{
		public TBOX_AbnormalAlarmResp(){
			super.appId = TBOX_AbnormalAlarm.appId;
			super.msgId = 2;
		}
		public byte[] getAppData(){
			byte[] b = {};
			return b;
		}		
		
		public byte[] getResp(){
			return null;
		}		
	}	
}
